#!/bin/tcsh -fe
####
#
#############################################################################
#                                                                           #
# Title: Renumber Imagenumbers                                              #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 11/25/2015                                             #
# Last Modification: 11/25/2015                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 10
#
# MANUAL: <B>Renumber Image Numbers<B>
#
# MANUAL: This script will renumber all images in the current project.
# MANUAL: With the switch you can decide if you want to do this.
#
# DISPLAY: renumber_imagenumbers_sure
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
# Variables to be available from the config file are:
#
set renumber_imagenumbers_sure = ""
#
#$end_vars
#
set scriptname = 2dx_renumber_images
#
\rm -f LOGS/${scriptname}.results
#
if ( ${renumber_imagenumbers_sure} == "no" ) then
  ${proc_2dx}/protest "Not running (Are you sure is 'no')."
endif
#
echo "<<@progress: 1>>"
# echo "<<@evaluate>>"
#
source ${proc_2dx}/2dx_makedirs
#
echo "# IMAGE: 2dx_merge_dirfile.dat <TXT: Directory List>" >> LOGS/${scriptname}.results
#
# This memorizes the current merge directory under the variable "olddir":
set olddir = $PWD
#
echo "::The current working directory is" ${olddir}
echo ": "
#
cd ..
set rootdir = $PWD
#
find . -name 2dx_image.cfg -print | sed 's/\/2dx_image.cfg//' > ${olddir}/SCRATCH/2dx_merge_alldirs.dat
cd ${olddir}
#
set dirnum = `cat SCRATCH/2dx_merge_alldirs.dat | wc -l`
echo ": Will work on ${dirnum} image directories."
set increment = `echo ${dirnum} | awk '{ s = 80.0 / ( $1 + 1 ) } END { print s }'`
set curval = 10.0
#
set imagenumber = 100
#
set dircounter = ${dirnum}
set irun = 1
while ( ${dircounter} > 0 )
  #
  ${bin_2dx}/2dx_getline.exe << eot > TMP.tmp
${olddir}/SCRATCH/2dx_merge_alldirs.dat
${dircounter}
eot
  #
  set from_dir = `cat TMP.tmp`
  \rm TMP.tmp
  #
  set newimagenumber = `echo ${imagenumber} | ${bin_2dx}/2dx_getnumber.exe`
  set testval = `echo ${newimagenumber} | wc -c`
  if ( `echo ${testval} | awk '{ if ( $1 < 11 ) { s = 1 } else { s = 0 }} END { print s }'` == 1 ) then
    set oldval = ${newimagenumber}
    set newimagenumber = `echo 0000000000 | cut -c${testval}-`${newimagenumber}
  endif  
  #
  if ( `echo ${testval} | awk '{ if ( $1 > 11 ) { s = 1 } else { s = 0 }} END { print s }'` == 1 ) then
    set oldval = ${newimagenumber}
    set startnum = `echo ${testval} | awk '{ s = $1 - 10 } END { print s }'`
    set endnum   = `echo ${testval} | awk '{ s = $1 - 1 } END { print s }'`
    set newimagenumber = `echo ${newimagenumber} | cut -c${startnum}-${endnum}`
  endif  
  #
  echo "::Setting imagenumber ${newimagenumber} for ${from_dir}"
  cd ${rootdir}
  cd ${from_dir}
  #
  if ( -e 2dx_image.cfg ) then
    # This tells the GUI that the parameters are now to be added in the current image directory
    echo "<IMAGEDIR="\"${PWD}\"">" >> ${olddir}/LOGS/${scriptname}.results
    #
    echo "set imagenumber = ${newimagenumber}" >> ${olddir}/LOGS/${scriptname}.results
    set fullpath = `pwd`
  else
    ${proc_2dx}/linblock "WARNING: ${source} does not exist here."
  endif
  #
  set progress = `echo ${irun} ${dirnum} | awk '{ s = 10 + int( 80 * $1 / $2 ) } END { print s }'` 
  echo "<<@progress:${progress}>>"
  #
  @ imagenumber += 100
  @ irun += 1
  @ dircounter -= 1
  cd ${olddir}
  # This tells the GUI to get back to the original dir for setting parameters
  echo "<IMAGEDIR>" >> ${olddir}/LOGS/${scriptname}.results
end
#
${proc_2dx}/linblock "Done."
echo "<<@progress: 100>>"
#
#
